//This is useful for remove an element from an array.
Array.prototype.remove = function(obj) {
    for (var i = 0; i < this.length; ++i) {
        if (this[i] == obj) {
            if (i > this.length / 2) {
                for (var j = i; j < this.length - 1; ++j) {
                    this[j] = this[j + 1];
                }
                this.pop();
            }
            else {
                for (var j = i; j > 0; --j) {
                    this[j] = this[j - 1];
                }
                this.shift();
            }
            break;
        }
    }
};  